import React, {PureComponent} from "react";
import {Table} from "antd";
import {connect} from "react-redux";
import {tableProps} from "../../lib/ui";
import {query} from "./actions";
import {groupBy} from "../../lib/func";
import {getLastFilter} from "../../lib/identity";

const LAST_FILTER = "ghOrderAnalysis";

class List extends PureComponent {

    componentWillMount() {
        const {dispatch} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        dispatch(query({...fields}));
    };


    render() {
        const {
            dataSource,
            loading,
        } = this.props;
        const columns = [];
        columns.push({
            title: "日期",
            dataIndex: "date",
            width: "100px"
        });
        dataSource.forEach(item => {
            const feeList = item.feeList;
            let groupData = groupBy(feeList, (link) => {
                return link.clientName + "(应收)"
            })
            for (let clientName in groupData) {
                let found = columns.find(c => c.title === clientName);
                let canAdd = false;
                if (!found) {
                    canAdd = true;
                    found = {
                        title: clientName,
                        children: []
                    };
                }
                const data = groupData[clientName];
                data.forEach(fee => {
                    const currency = fee.currency;
                    let foundItem = found.children.find(c => c.title === currency);
                    if (!foundItem) {
                        found.children.push({
                            title: currency,
                            dataIndex: clientName + currency,
                            width: "60px"
                        });
                    }
                    item[clientName + currency] = fee.feeCount;
                });
                if (canAdd) {
                    columns.push(found);
                }
            }
            const outFeeList = item.outFeeList;
            groupData = groupBy(outFeeList, (link) => {
                return link.carrierName + "(应付)"
            })
            for (let carrierName in groupData) {
                let found = columns.find(c => c.title === carrierName);
                let canAdd = false;
                if (!found) {
                    canAdd = true;
                    found = {
                        title: carrierName,
                        children: []
                    };
                }
                const data = groupData[carrierName];
                data.forEach(fee => {
                    const currency = fee.currency;
                    let foundItem = found.children.find(c => c.title === currency);
                    if (!foundItem) {
                        found.children.push({
                            title: currency,
                            dataIndex: carrierName + currency,
                            width: "60px"
                        });
                    }
                    item[carrierName + currency] = fee.feeCount;
                });
                if (canAdd) {
                    columns.push(found);
                }
            }
        });

        return (
            <Table
                {...tableProps}
                columns={columns}
                scroll={{x: 1500, y: 'calc(100vh - 480px)'}}
                dataSource={dataSource}
                loading={loading}
            />
        );
    }
}

const
    mapStateToProps = state => {
        return {
            ...state.ghOrderAnalysis.list
        };
    };

export default connect(mapStateToProps)(List);
